<?php

namespace Admin\Mapper;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\ServiceManager\ServiceManager;
use User\Mapper\User;

class AdminUser extends User implements AdminUserInterface
{
    const ITEM_PER_PAGE = 20;
    const PAGE_RANGE = 10;
    const ROLE_ADMIN = 1;
    
    public function fetchAll($page)
    {
        /*
        $select = $this->getSelect();
        $select->join('user_role_linker' , 'user.user_id = user_role_linker.user_id');
        $users = $this->select($select);
        //die(print_r($users));*/
        $select = $this->getSelect();
        //$select->columns(array('user' , 'user_role'));
        //$select->columns(array('user.*', 'roles' => 'user.name'))
        $select->join('user_role_linker' , 'user.user_id = user_role_linker.user_id');
        $select->join('user_role' , 'user_role_linker.role_id = user_role.id');
        //$select->offset(0)->limit(static::ITEM_PER_PAGE);
       /* $users = $this->select($select);
        $users->buffer();

        $paginator = new Paginator(new paginatorIterator($users));
        //$paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
        $paginator->setCurrentPageNumber($page)
            ->setItemCountPerPage(1)
            ->setPageRange(100);*/
        $paginator = $this->paginator($select , $page , static::ITEM_PER_PAGE , static::PAGE_RANGE);
        return $paginator;
        die(print_r($paginator));
        /*$stmt = $this->getSlaveSql()->prepareStatementForSqlObject($select);

        //$result = $stmt->execute();

        if ($result) {
            //die(print_r($roles));
            $resultSet = new \Zend\Db\ResultSet\HydratingResultSet(new \Zend\Stdlib\Hydrator\Reflection, new \User\Entity\User());
            $resultSet->initialize($result);
            //var_dump($resultSet);
            foreach ($resultSet as $user) {
                var_dump($user);
                $role = new \Authorize\Entity\Role;
                $role->setId($user->getRoleId());
                //$role->setRoleId($user->getRoleId());
                //$user->addRole($role);
                $users[] = $user;
                //echo $user->getFirstName() . ' ' . $user->getLastName() . PHP_EOL;
            }
        }
        /*
        $sql = $this->getSql();

        $select = $this->getSelect();
        $select->join('user_role_linker' , 'user.user_id = user_role_linker.user_id');

        //you can check your query by echo-ing :
        // echo $select->getSqlString();
        $statement = $this->getSlaveSql()->prepareStatementForSqlObject($select);
        $users = $statement->execute();*/

        return $users;
    }

    public function fetchPaginator($page)
    {
        $select = $this->getSelect();
        $select->join('user_role_linker' , 'user.user_id = user_role_linker.user_id' ,array('role_id') , 'left');
        $select->join(array('roles' => 'user_role') , 'user_role_linker.role_id = roles.id' , array('role_name' => 'role_id') , 'left');
        $select->join('images_links' , 'images_links.object_id = user.user_id' ,array('pair_id') , 'left');
        $select->join('images', 'images_links.image_id = images.image_id' , array('image_id','image_path') , 'left');
        $select->group(array('user.user_id')); 
        $select->order('user.user_id DESC');
        
        $paginator = $this->paginator($select , $page , static::ITEM_PER_PAGE , static::PAGE_RANGE);
        return $paginator;
    }
    
    public function findById($id)
    {
        $select = $this->getSelect();
        $select->join('user_role_linker' , 'user.user_id = user_role_linker.user_id' ,array('role_id') , 'left');
        $select->join(array('roles' => 'user_role') , 'user_role_linker.role_id = roles.id' , array('role_name' => 'role_id') , 'left');
        $select->join('images_links' , 'images_links.object_id = user.user_id' ,array('pair_id') , 'left');
        $select->join('images', 'images_links.image_id = images.image_id' , array('image_id','image_path') , 'left');
        $select->group(array('user.user_id')); 
        $select->where(array('user.user_id' => $id));

        $entity = $this->select($select)->current();
        $this->getEventManager()->trigger('find', $this, array('entity' => $entity));
        return $entity;
    }

    public function update($entity, $where = null, $tableName = null, HydratorInterface $hydrator = null)
    {
        //die(print_r($entity));

        return parent::update($entity, $where, $tableName, $hydrator);
    }

    public function delete($where, $tableName = null)
    {
        return parent::delete($where, $tableName);
    }
    
    public function findAdminByUsername($username)
    {
        
    }
    
    public function findAdminByEmail($email)
    {
        $select = $this->getSelect();
        $select->join('user_role_linker' , 'user.user_id = user_role_linker.user_id');
        //$select->join('user_role' , 'user_role_linker.role_id = user_role.id');
        $select->where(array('user.email' => $email , 'user_role_linker.role_id' => static::ROLE_ADMIN));

        $entity = $this->select($select)->current();
        $this->getEventManager()->trigger('find', $this, array('entity' => $entity));
        return $entity;
    }
}
